<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <title>登陆/注册</title>
</head>
<body>
    <h3>LOGIN</h3>
    <form action="javascript:;">
        用户名:<input type="text" name="username" id="username" value=""><br/>
        密 码:<input type="password" name="password" id="password" value=""><br/>
        <button id="login">登陆</button>
    </form>

    <script>
        window.onload = function () {
            function getUrl(url) {
                let base = 'http://localhost:8080';
                return `${base}${url}`;
            }
            let oBtn = document.querySelector('#login');
            oBtn.addEventListener('click', function () {
                let url = getUrl('/login');
                let headers = new Headers();
                headers.append('content-type', 'application/json;charset=UTF-8');
                let username = document.querySelector('#username').value;
                let password = document.querySelector('#password').value;
                let param = {
                    method: 'POST',
                    headers: headers,
                    body: JSON.stringify({'username': username, 'password': password})
                };
                fetch(url, param)
                    .then(result => {
                      if(result.ok) {
                          result.text().then(res => {
                              console.log(res)
                          });
                      } else {
                          // 刷新当前页
                          // window.history.go(0);
                          window.location.reload();
                      }
                    }).catch(e => {
                        console.log(e)
                });
            });
        }
    </script>
</body>
</html>